package xcmg.device.web.warehouse;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.warehouse.WarehouseDealerAuthParamVO;
import xcmg.device.service.vo.warehouse.WarehouseDealerAuthVO;
import xcmg.device.service.warehouse.WarehouseDealerAuthService;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户授权信息
 * 对外提供接口
 * @Author huhuawei
 */
@RestController
@RequestMapping("/warehouseDealerAuth")
public class WarehouseDealerAuthController extends BasicController{

    @Autowired
    private WarehouseDealerAuthService warehouseDealerAuthService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "增加客户授权信息")
    @FastMappingInfo(needLogin = true,code= AuthCode.Depot.ADD)
    public ActionResult addWarehouseDealerAuth(@RequestBody WarehouseDealerAuthParamVO warehouseDealerAuthParamVO) throws Exception{
       //判断相应的参数 不能为空
        if(warehouseDealerAuthParamVO.getDealerAuthIds().length ==0
                || warehouseDealerAuthParamVO.getSupplierId().isEmpty()
                || warehouseDealerAuthParamVO.getChecked()== null){
            return actionResult(ErrorCode.IllegalArument);
        }
        List<WarehouseDealerAuthVO> listVO=new ArrayList<>();
        for(String dealerAuthId: warehouseDealerAuthParamVO.getDealerAuthIds()){
            WarehouseDealerAuthVO vo =new WarehouseDealerAuthVO();
            vo.setSupplierId(warehouseDealerAuthParamVO.getSupplierId());
            vo.setDealerAuthId(dealerAuthId);
            vo.setChecked(warehouseDealerAuthParamVO.getChecked());
            vo.setDealerId(getOrgId());
            listVO.add(vo);
        }
        return actionResult(warehouseDealerAuthService.addWarehouseDealerAuth(listVO));
    }


    @RequestMapping(value = "/list",method = RequestMethod.GET )
    @ApiOperation(value = "分页查询客户授权信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult listWarehousesDealerAuth(int pageNum, int pageSize,WarehouseDealerAuthVO warehouseDealerAuthVO) throws  Exception{
        PageCommonVO pageCommonVO = warehouseDealerAuthService.listWarehousesDealerAuth(pageNum, pageSize,warehouseDealerAuthVO);
        return actionValueResult(pageCommonVO.getPageInfo());
    }
}
